package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.dao.DoctorAuthDAO;
import com.model._do.DoctorAuthDO;
import com.model._do.UserDO;
import com.model.dto.SuperviseDTO.AuthDoctorDTO;
import com.model.dto.SuperviseDTO.Search.AuthDoctorSearch;
import com.service.DoctorAuthService;
import com.service.UserService;
import com.util.LocationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("doctorAuthService")
public class DoctorAuthServiceImpl implements DoctorAuthService {

    @Autowired
    private DoctorAuthDAO doctorAuthDAO;

    @Autowired
    private UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void upLoadAuthFile(MultipartFile file, DoctorAuthDO auth, HttpServletRequest req ) throws IOException {
        // 判断文件是否为空，空则返回失败页面
        // 获取文件存储路径（绝对路径）
        String path = "/MEDICAL_AUTH/file";
        // 获取原文件名
        String fileName = file.getOriginalFilename();
        // 创建文件实例
        File filePath = new File(path, fileName);
        // 如果文件目录不存在，创建目录
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        // 写入文件
        file.transferTo(filePath);
        auth.setAuthFile(path + "/" + fileName);
        doctorAuthDAO.uploadAuth(auth);
    }

    @Override
    public DoctorAuthDO getDoctorByUserId(UserDO doctor) {
        return doctorAuthDAO.getDoctorByUserId(doctor);
    }

    @Override
    public List<AuthDoctorDTO> getAuthInfo(AuthDoctorSearch search) {
        if(search.getType().equals("done")){
            List<DoctorAuthDO> doneList = doctorAuthDAO.getAuthDone(search);
            return doneList.stream().map(d -> this.tranToDTO(d, search)).filter(Objects::nonNull).collect(Collectors.toList());
        } else if(search.getType().equals("unDone")){
            List<DoctorAuthDO> undonelist = doctorAuthDAO.getAuthUnDone(search);
            return undonelist.stream().map(d -> this.tranToDTO(d, search)).filter(Objects::nonNull).collect(Collectors.toList());
        } else {
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pushAuthResult(DoctorAuthDO auth) {
        doctorAuthDAO.pushAuthResult(auth);
        UserDO user = new UserDO();
        user.setUserId(auth.getUserId());
        userService.updateUser(user);
    }

    private AuthDoctorDTO tranToDTO(DoctorAuthDO doctorAuth, AuthDoctorSearch search){
        if(search.getUserId() != null && !doctorAuth.getUserId().equals(search.getUserId())){
            return null;
        }
        UserDO user = userService.getUserByUserId(doctorAuth.getUserId());
        if(LocationUtil.filterLocation(JSON.parseObject(user.getUserLocation()), search)){

        } else {
            return null;
        }
        AuthDoctorDTO dto = new AuthDoctorDTO();
        dto.setAuth(doctorAuth);
        dto.setDoctor(user);
        return dto;
    }
}
